<?php

class UploadedFile {

    /**
     * @var MimeType
     */
    private $oMimeType;

    /**
     * @return MimeType
     */
    public function getMimeType() { return $this->oMimeType; }

    private $path;
    public function getPath() { return $this->path; }

    protected function __construct(MimeType $oMimeType, $path) {
        $this->oMimeType = $oMimeType;
        $this->path = $path;
    }

    /**
     * @return UploadedFile
     */
    public static function createIfExists() {

        if (empty($_FILES['file']))
            return new ErrorUploadedFile(Tr::uploadError('NO_FILES'));

        $hFile = $_FILES['file'];

        if ($hFile['error'] !== 0)
            return new ErrorUploadedFile(Tr::uploadError($hFile['error']));

        if (!is_uploaded_file($hFile['tmp_name']))
            return new ErrorUploadedFile(Tr::uploadError('IS_NOT_UPLOADED_FILE'));

        if ($_FILES['file'])

        return new UploadedFile(
            new MimeType($hFile['type']),
            $hFile['tmp_name']
        );
    }

    public function isError()  { return false; }
    public function getError() { return ''; }
}

class ErrorUploadedFile extends UploadedFile {

    private $reason;
    public function __construct($reason) {
        $this->reason = $reason;
    }

    public function getMimeType() { throw new Exception('not implemented for class' . get_class()); }
    public function getPath() { throw new Exception('not implemented for class' . get_class()); }

    public function isError()  { return true; }
    public function getError() { return $this->reason; }

}
?>